#include <stdio.h>
int main()

{
	int a[1000]={0};
	int ret = 0;
	int count = 0;
	int i,j;
	for(i = 0;i < 1000;i++)
	{
		scanf("%d",&a[i]);
		if(a[i] == 0)break;
	}
	for(j = 0;j < i;j++)
	{
		if(a[j] == 1)
		{
		   count++;
		   ret = 0;
	    }
		else if(a[j] == 2)
		{
			ret += 2;
			count += ret;
		}
	}
	printf("%d",count);
	return 0;
}